﻿using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class StatBonus
{
	public StatType statType;
	public BodyType bodyType;
	public float minValue;
	public float maxValue;

	public string GetDisplayString()
	{
		return statType.ToString() + " " + ((minValue + maxValue) / 2f);
	}
	
	#region Editor
	public bool Draw()
	{
		bool singleValue = false;
		string divider = "";
		switch (statType)
		{
		case StatType.Initiative:
		case StatType.Range:
		case StatType.Speed:
		case StatType.Qty_Limit:
		case StatType.Usage_Limit:
			singleValue = true;
			break;

		case StatType.Health:
			divider = "/";
			break;

		default:
			divider = "-";
			break;
		}

		// Light Gray
		GUI.color = (Color.gray + Color.white) / 2f;
		EditorGUILayout.BeginHorizontal("box");
		{
			GUI.color = Color.white;
			EditorGUILayout.BeginVertical();
			{
				EditorGUILayout.BeginHorizontal();
				{
					statType = (StatType)EditorGUILayout.EnumPopup(statType);
					if (statType == StatType.Health)
					{
						bodyType = (BodyType)EditorGUILayout.EnumPopup(bodyType);
					}
				}
				EditorGUILayout.EndHorizontal();
				EditorGUILayout.BeginHorizontal();
				{
					if (!singleValue)
					{
						minValue = EditorGUILayout.FloatField(minValue);
						EditorGUILayout.LabelField(divider, GUILayout.Width(10));
						maxValue = EditorGUILayout.FloatField(maxValue);
					}
					else
					{
						minValue = EditorGUILayout.FloatField(minValue);
					}
				}
				EditorGUILayout.EndHorizontal();
			}
			EditorGUILayout.EndVertical();
			if (GUILayout.Button("X", GUILayout.Width(20)))
			{
				return true;
			}
		}
		EditorGUILayout.EndHorizontal();

		return false;
	}
	#endregion
	
	public StatBonus Clone()
	{
		StatBonus bonus = new StatBonus();
		bonus.statType = statType;
		bonus.bodyType = bodyType;
		bonus.minValue = minValue;
		bonus.maxValue = maxValue;
		return bonus;
	}
}

#if UNITY_EDITOR
	[CustomPropertyDrawer(typeof(StatBonus))]
	public class BonusDrawer : PropertyDrawer
{
	public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
	{
		EditorGUI.BeginProperty(position, label, property);
		
		float indentLength = (15f * EditorGUI.indentLevel) / position.width;
		Rect rect = new Rect(position);
		
		rect.width = position.width * 0.3f;
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("statType"), GUIContent.none);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.7f + indentLength);
		StatType statType = (StatType)property.FindPropertyRelative("statType").enumValueIndex;
		switch (statType)
		{
		case StatType.Health:
			DrawHealth(rect, property);
			break;
			
		case StatType.Strength:
		case StatType.Precision:
		case StatType.Defense:
			DrawRange(rect, property);
			break;
			
		case StatType.Initiative:
		case StatType.Speed:
		case StatType.Range:
			DrawSingle(rect, property);
			break;
		}
		
		EditorGUI.EndProperty();
	}
	
	private void DrawHealth(Rect position, SerializedProperty property)
	{
		float indentLength = (15f * EditorGUI.indentLevel) / position.width;
		Rect rect = new Rect(position);
		
		rect.width = position.width * 0.4f;
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("bodyType"), GUIContent.none);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.25f + indentLength);
		EditorGUI.LabelField(rect, "");
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.15f + indentLength);
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("minValue"), GUIContent.none);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.05f + indentLength);
		EditorGUI.LabelField(rect, "/", EditorStyles.boldLabel);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.15f + indentLength);
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("maxValue"), GUIContent.none);
	}
	
	private void DrawRange(Rect position, SerializedProperty property)
	{
		float indentLength = (15f * EditorGUI.indentLevel) / position.width;
		Rect rect = new Rect(position);
		
		rect.width = position.width * 0.65f;
		EditorGUI.LabelField(rect, "");
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.15f + indentLength);
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("minValue"), GUIContent.none);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.05f + indentLength);
		EditorGUI.LabelField(rect, "-", EditorStyles.boldLabel);
		
		rect.x += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.15f + indentLength);
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("maxValue"), GUIContent.none);
	}
	
	private void DrawSingle(Rect position, SerializedProperty property)
	{
		float indentLength = (15f * EditorGUI.indentLevel) / position.width;
		Rect rect = new Rect(position);
		
		rect.width = position.width * 0.85f;
		EditorGUI.LabelField(rect, "");
		
		rect.x  += rect.width - 15f * EditorGUI.indentLevel;
		rect.width = position.width * (0.15f + indentLength);
		EditorGUI.PropertyField(rect, property.FindPropertyRelative("minValue"), GUIContent.none);
	}
}
#endif
